<?php
namespace App\Http\Controllers\Laifei\Vip;

use App\Http\Controllers\Controller;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaifeiVipDetailController extends Controller
{

    public function index()
    {

        $all = $this->all;

        $v_id = $all['v_id'];
        try{

            $result = DB::table('laifei_vip as lv')
                ->leftJoin('laifei_sys_vip as lsv','lv.vip_id','=','lsv.vip_id')
                ->leftJoin('lottery_users as lu','lv.we_id','=','lu.we_id')
                ->leftJoin('laifei_adviser_user as lau',function($join){
                    $join->on('lv.we_id','=','lau.we_id')->where('lau.is_delete','=',0);
                })
                ->leftJoin('laifei_sys_adviser as lsa','lau.ad_id','=','lsa.ad_id')
                ->where('lv.v_id','=',$v_id)
                ->select('lv.v_id','lv.we_id','lv.vip_id','lv.total_month','lv.continue_times','lv.start_time','lv.end_time','lv.created_at as begin_time',
                    'lsv.title','lsv.subtitle','lu.openid','lu.nickname','lu.sex','lu.phone','lu.headimgurl','lu.subscribe_time','lsa.ad_id','lsa.name as adviser')
                ->get()
                ->map(function($item){
                    $item->status = strtotime($item->end_time) > time();
                    $item->status_str = $item->status? '有效': '无效';
                    $item->sex_str = Data::$dataSex[$item->sex];
                    return $item;
                })
                ->toArray();
            $result = json_decode(json_encode($result),true);
            $result = $result[0];

            return $this->setReturnJson(0,'success',$result);
        } catch (\Exception $e) {
            Log::error(LaifeiVipStoreController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败');
        }

    }

}